//商品分类全局组件的小仓库
import { defineStore } from "pinia";
import {
  getResourceOne,
  getResourceTwo,
  getResourceThree,
} from "@/api/classification";
import type { ApiResponse, classifyInfo } from "@/api/classification/type";
import type { CategoryState } from "./types/type";
const useCategoryStore = defineStore("Category", {
  state: (): CategoryState => {
    return {
      //存储一级分类的数据
      c1Arr: [],
      //存储一级分类的ID
      c1Id: 0,
      //存储对应一级分类下二级分类的数据
      c2Arr: [],
      //收集二级分类的ID
      c2Id: 0,
      //存储三级分类的数据
      c3Arr: [],
      //存储三级分类的ID
      c3Id: 0,
    };
  },
  actions: {
    //获取一级分类的方法
    async getC1() {
      //发请求获取一级分类的数据
      const result: ApiResponse = await getResourceOne();
      if (Number(result.code) === 20000) {
        this.c1Arr = result.data.resourceTypes;
      }
    },
    //获取二级分类的数据
    async getC2() {
      //获取对应一级分类的下二级分类的数据
      const result: ApiResponse = await getResourceTwo(this.c1Id);
      if (Number(result.code) === 20000) {
        this.c2Arr = result.data.resourceTypes;
      }
    },

    //获取三级分类的数据
    async getC3() {
      const result: ApiResponse = await getResourceThree(this.c2Id);
      if (Number(result.code) === 20000) {
        this.c3Arr = result.data.resourceTypes;
      }
    },
  },
  getters: {},
});

export default useCategoryStore;
